package com.huarui.companypackage.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.company.model.CompanyMarket;
import com.huarui.companypackage.dao.ICompanyMarketDao;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class CompanyMarketDaoImpl extends BaseDaoImpl implements ICompanyMarketDao {

	@Override
	public void batchInsertCompanyMarket(final List<CompanyMarket> companyMarkets) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch=0;
				for(CompanyMarket companyMarket :companyMarkets){
					executor.insert(namespace+".create", companyMarket);
					batch++;
					if(batch == 20){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}

	public Date getCurDay(){
		return (Date) getSqlMapClientTemplate().queryForObject(namespace+".getCurDay");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CompanyMarket> getPublishableCompanyMarketByCompanyId(Integer companyId) {
		return getSqlMapClientTemplate().queryForList(namespace+".getPublishableCompanyMarketByCompanyId", companyId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CompanyMarket> getSearchableCompanyMarketByCompanyId(Integer companyId) {
		return getSqlMapClientTemplate().queryForList(namespace+".getSearchableCompanyMarketByCompanyId", companyId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CompanyMarket> getAllCompanyMarketByCompanyId(Integer companyId) {
		return getSqlMapClientTemplate().queryForList(namespace+".getAllCompanyMarketByCompanyId", companyId);
	}

	@Override
	public void batchUpdateCompanyMarket(final List<CompanyMarket> companyMarkets) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch=0;
				for(CompanyMarket companyMarket :companyMarkets){
					executor.insert(namespace+".update", companyMarket);
					batch++;
					if(batch == 20){
						executor.executeBatch();
						batch = 0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});	
	}
	
}
